home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- __revision__ = '$Id: xmppstringprep.py,v 1.16 2004/10/07 22:28:04 jajcus Exp $'
- __docformat__ = 'restructuredtext en'
- import stringprep
- import unicodedata
- from pyxmpp.exceptions import StringprepError
-
- class LookupFunction:
-
- def __init__(self, function):
- self.lookup = function
-
-
-
- class LookupTable:
-
- def __init__(self, singles, ranges):
- self.singles = singles
- self.ranges = ranges
-
-
- def lookup(self, c):
- if self.singles.has_key(c):
- return self.singles[c]
-
- c = ord(c)
- for start, end in self.ranges:
- value = None
- if c < start:
- return None
-
- if c <= end:
- return value
- continue
-
-
-
- A_1 = LookupFunction(stringprep.in_table_a1)
-
- def b1_mapping(uc):
- if stringprep.in_table_b1(uc):
- return u''
- else:
- return None
-
- B_1 = LookupFunction(b1_mapping)
- B_2 = LookupFunction(stringprep.map_table_b2)
- B_3 = LookupFunction(stringprep.map_table_b3)
- C_1_1 = LookupFunction(stringprep.in_table_c11)
- C_1_2 = LookupFunction(stringprep.in_table_c12)
- C_2_1 = LookupFunction(stringprep.in_table_c21)
- C_2_2 = LookupFunction(stringprep.in_table_c22)
- C_3 = LookupFunction(stringprep.in_table_c3)
- C_4 = LookupFunction(stringprep.in_table_c4)
- C_5 = LookupFunction(stringprep.in_table_c5)
- C_6 = LookupFunction(stringprep.in_table_c6)
- C_7 = LookupFunction(stringprep.in_table_c7)
- C_8 = LookupFunction(stringprep.in_table_c8)
- C_9 = LookupFunction(stringprep.in_table_c9)
- D_1 = LookupFunction(stringprep.in_table_d1)
- D_2 = LookupFunction(stringprep.in_table_d2)
-
- def nfkc(data):
- if type(data) is list:
- data = u''.join(data)
-
- return unicodedata.normalize('NFKC', data)
-
-
- class Profile:
- cache_items = []
-
- def __init__(self, unassigned, mapping, normalization, prohibited, bidi = 1):
- self.unassigned = unassigned
- self.mapping = mapping
- self.normalization = normalization
- self.prohibited = prohibited
- self.bidi = bidi
- self.cache = { }
-
-
- def prepare(self, data):
- r = self.cache.get(data)
- if r is not None:
- return r
-
- s = self.map(data)
- if self.normalization:
- s = self.normalization(s)
-
- s = self.prohibit(s)
- s = self.check_unassigned(s)
- if self.bidi:
- s = self.check_bidi(s)
-
- if type(s) is list:
- s = u''.string.join()
-
- if len(self.cache_items) >= stringprep_cache_size:
- remove = self.cache_items[:-stringprep_cache_size / 2]
- for profile, key in remove:
-
- try:
- del profile.cache[key]
- continue
- except KeyError:
- continue
-
-
-
- self.cache_items[:] = self.cache_items[-stringprep_cache_size / 2:]
-
- self.cache_items.append((self, data))
- self.cache[data] = s
- return s
-
-
- def prepare_query(self, s):
- s = self.map(s)
- if self.normalization:
- s = self.normalization(s)
-
- s = self.prohibit(s)
- if self.bidi:
- s = self.check_bidi(s)
-
- if type(s) is list:
- s = u''.string.join(s)
-
- return s
-
-
- def map(self, s):
- r = []
- for c in s:
- rc = None
- for t in self.mapping:
- rc = t.lookup(c)
- if rc is not None:
- break
- continue
-
- if rc is not None:
- r.append(rc)
- continue
- r.append(c)
-
- return r
-
-
- def prohibit(self, s):
- for c in s:
- for t in self.prohibited:
- if t.lookup(c):
- raise StringprepError, 'Prohibited character: %r' % (c,)
- continue
-
-
- return s
-
-
- def check_unassigned(self, s):
- for c in s:
- for t in self.unassigned:
- if t.lookup(c):
- raise StringprepError, 'Unassigned character: %r' % (c,)
- continue
-
-
- return s
-
-
- def check_bidi(self, s):
- has_l = 0
- has_ral = 0
- for c in s:
- if D_1.lookup(c):
- has_l = 1
- continue
- if D_2.lookup(c):
- has_l = 1
- continue
-
- if has_l and has_ral:
- raise StringprepError, 'Both RandALCat and LCat characters present'
-
- if has_l:
- if D_1.lookup(s[0]) is None or D_1.lookup(s[-1]) is None:
- raise StringprepError, 'The first and the last character must be RandALCat'
-
- return s
-
-
- nodeprep = Profile(unassigned = (A_1,), mapping = (B_1, B_2), normalization = nfkc, prohibited = (C_1_1, C_1_2, C_2_1, C_2_2, C_3, C_4, C_5, C_6, C_7, C_8, C_9, LookupTable({
- u'"': True,
- u'&': True,
- u"'": True,
- u'/': True,
- u':': True,
- u'<': True,
- u'>': True,
- u'@': True }, ())), bidi = 1)
- resourceprep = Profile(unassigned = (A_1,), mapping = (B_1,), normalization = nfkc, prohibited = (C_1_2, C_2_1, C_2_2, C_3, C_4, C_5, C_6, C_7, C_8, C_9), bidi = 1)
- stringprep_cache_size = 1000
-
- def set_stringprep_cache_size(size):
- global stringprep_cache_size
- stringprep_cache_size = size
- if len(Profile.cache_items) > size:
- remove = Profile.cache_items[:-size]
- for profile, key in remove:
-
- try:
- del profile.cache[key]
- continue
- except KeyError:
- continue
-
-
-
- Profile.cache_items = Profile.cache_items[-size:]
-
-
-